package com.situ.mall.controller.front;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.User;
import com.situ.mall.service.IUserService;
import com.situ.mall.util.JSONResult;

@Controller
@RequestMapping("/user")
public class FrontUserController {
	@Autowired
	private IUserService userService;
	

	@RequestMapping("/checkUserLogin.shtml")
	@ResponseBody
	public JSONResult checkUserLogin(HttpSession session) {
		System.out.println("FrontUserController.checkUserLogin()");
		User user = (User) session.getAttribute("user");
//		if (user != null) {
//			return JSONResult.ok();
//		} else {
//			return JSONResult.error();
//		}
		
		return user!=null ? JSONResult.ok() : JSONResult.error(); 
	}
	
	@RequestMapping("/login.shtml")
	@ResponseBody
	public JSONResult login(String name, String password, HttpSession session) {
		System.out.println("FrontUserController.login()");
		User user = userService.login(name, password);
		if (user != null) {
			session.setAttribute("user", user);
			return JSONResult.ok("登录成功");
		} else {
			return JSONResult.error("登录失败");
		}
	}
}
